﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HelpDesk.Models;
using System.Web.Security;
using System.Data.Objects.SqlClient;
using System.ComponentModel;
using System.IO;

namespace HelpDesk.Controllers
{
    [Authorize]
    public class TicketController : Controller
    {
        private HelpDeskDbContext _db = HelpDeskDbContext.Instance;

        //
        // GET: /Ticket/

        public ActionResult Index()
        {
            var model = Ticket.GetAllTickets();
            return View(model);
        }


        public ActionResult Details(int id = 0)
        { 
            Ticket ticket = Ticket.GetTicketByID(id);
            if (ticket == null)
            {
                return HttpNotFound();
            }
            return View(ticket);
        }

        [HttpPost]
        public PartialViewResult CloseTicket(int id = 0)
        {
            var modalBoxViewModel = new ModalBoxViewModel();
            var ticket = Ticket.GetTicketByID(id);
            if (ticket != null)
            {
                //Close Ticket
                ticket.Status = TicketStatus.GetTicketByID(4);


                if (TryUpdateModel(ticket))
                {
                    if (ModelState.IsValid)
                    {
                        UpdateModel(ticket);
                        _db.SaveChanges();
                        modalBoxViewModel.Message = new HtmlString("Ticket# <span class=\"label label-success\">" + id + "</span> has been closed successfully.");
                    }
                    else
                    {
                        modalBoxViewModel.Message = new HtmlString(HelpDeskDbContext.ParseModelStateErrors(ModelState.Values));

                    }
                }
                else
                {
                    modalBoxViewModel.Message = new HtmlString(HelpDeskDbContext.ParseModelStateErrors(ModelState.Values));
                }
                
            }
            else
            {
                modalBoxViewModel.Message = new HtmlString("Ticket was not found in the database.");
            }



            return PartialView("_ModelBoxContent", modalBoxViewModel);
        }

        [HttpGet]
        public ActionResult AddActivity(int id = 0)
        {
            ViewBag.TicketID = id;
            if (id == -1)
            {
                return HttpNotFound();
            }
            return View();
        }

        [HttpPost]
        public ActionResult AddActivity(Activity activity)
        {
            try 
            {
                var tempTicketID = RouteData.Values["id"];
                int intTicket = 0;
                bool blnTicketValid = Int32.TryParse(tempTicketID.ToString(), out intTicket);
                if(tempTicketID == null && !blnTicketValid)
                {
                    return RedirectToAction("Index", "Ticket");
                }

                
                string strUser = User.Identity.Name;
                var loggedInUser = _db.UserProfiles.Where(u => u.UserName == strUser).FirstOrDefault();
                var ticketInfo = _db.Tickets.Where(t => t.TicketID == intTicket).FirstOrDefault();
                

                activity.Ticket = ticketInfo;
                activity.Created = DateTime.Now;
                activity.SubmittedBy = loggedInUser;

                if (ModelState.IsValid)
                {
                    Activity.AddActivity(activity);
                    return RedirectToAction("Details", new { id = ticketInfo.TicketID });
                }

                string msg = HelpDeskDbContext.ParseModelStateErrors(ModelState.Values);
                ViewBag.ErrorsMsg = msg;
               
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(String.Empty, ex);
            }

            return View();
        }

        
        public ActionResult CreateTicket()
        {

            return View();
        }

        [HttpPost]
        public ActionResult CreateTicket(Ticket ticket)
        {

            return View();
        }

        public PartialViewResult UploadFile(int id = 0)
        {

            return PartialView();
        }
    }
}
